
/*
 Initial code taken from : Quodlibet.be
 */
package org.iabako.server.tools.pdftable;


import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PdfParagraph {
    private int width = 500;
    private String text;
    private PDFont font = PDType1Font.HELVETICA;
    private int fontSize = 10;

    private int color = 0;

    public PdfParagraph(String text, PDFont font, int fontSize, int width) {
        this.text = text;
        this.font = font;
        this.fontSize = fontSize;
        this.width = width;
    }


    public List<String> getLines() throws IOException {
        List<String> result = new ArrayList();

        if (text == null) {
            return result;
        }

        for (String line : text.split("\n")) {

            int fontSize = this.fontSize;
            boolean smallText = line.startsWith("_small_text_");
            if (smallText) {
                line = line.replace("_small_text_", "");
                fontSize = 6;
            }

            String[] split = line.split("(?<=\\W)");
            int[] possibleWrapPoints = new int[split.length];
            possibleWrapPoints[0] = split[0].length();
            for (int i = 1; i < split.length; i++) {
                possibleWrapPoints[i] = possibleWrapPoints[i - 1] + split[i].length();
            }

            int start = 0;
            int end = 0;
            for (int i : possibleWrapPoints) {
                float width = font.getStringWidth(line.substring(start, i)) / 1000 * fontSize;
                if (start < end && width > this.width) {
                    String subLine = line.substring(start, end);
                    if (smallText && start == 0) {
                        subLine = "_small_text_" + subLine;
                    }
                    result.add(subLine);
                    start = end;
                }
                end = i;
            }

            //Do this if there's only one small text line
            if (smallText && start == 0) {
                line = "_small_text_" + line;
            }

            // Last piece of text
            result.add(line.substring(start));
        }
        return result;
    }

    public float getFontHeight() {
        return font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
    }

    public float getFontWidth() {
        return font.getFontDescriptor().getFontBoundingBox().getWidth() / 1000 * fontSize;
    }

    public PdfParagraph withWidth(int width) {
        this.width = width;
        return this;
    }

    public PdfParagraph withFont(PDFont font, int fontSize) {
        this.font = font;
        this.fontSize = fontSize;
        return this;
    }

    public PdfParagraph withColor(int color) {
        this.color = color;
        return this;
    }

    public int getColor() {
        return color;
    }

    public int getWidth() {
        return width;
    }

    public String getText() {
        return text;
    }

    public PDFont getFont() {
        return font;
    }

    public int getFontSize() {
        return fontSize;
    }
}
